// use jsx to render html, do not modify simple.html
import { ScrollAnim } from 'abc-ui';
import React from 'react';
import ReactDOM from 'react-dom';
import QueueAnim from 'rc-queue-anim';
import TweenOne from 'rc-tween-one';
import Animate from 'rc-animate';

import './assets/index.less';

const _package = require('../../package.json');

const Link = ScrollAnim.Link;
const Element = ScrollAnim.Element;
const ScrollOverPack = ScrollAnim.OverPack;
const EventListener = ScrollAnim.Event;
// ScrollAnim.scrollScreen({scrollInterval: 600});
class Demo extends React.Component {
  componentDidMount() {
    // 添加改变窗口事件,可加setTimeout
    EventListener.addEventListener(
      'resize.userResize',
      this.barAnimate.bind(this),
    );
  }
  componentWillUnmount() {
    EventListener.removeEventListener(
      'resize.userResize',
      this.barAnimate.bind(this),
    );
  }

  onFocus = e => {
    console.log(e, 'focus');
    this.dom = e.target;
    this.barAnimate();
  };

  onBlur = e => {
    console.log(e, 'blur');
  };

  onChange = e => {
    console.log(e);
  };

  barAnimate = () => {
    if (!this.dom) {
      return;
    }
    const bar = this.bar;
    bar.style.left = `${this.dom.getBoundingClientRect().left}px`;
  };

  render() {
    return (
      <div>
        <div className="nav">
          <div className="logo">
            <p>Ant Motion</p>
          </div>
          <div className="nav-wap">
            {/*
              showHeightActive 滚动到距顶部还有50%的时, link 标签被选中同时附于 active 值;在出场时是还有 50% 时 link 标签移除 active 值; 如果为Array时，第一个为进场，第二个为出场
              */}
            <Link
              className="nav-list"
              to="page0"
              showHeightActive={['50%', '10%']}
              onFocus={this.onFocus}
              onBlur={this.onBlur}
            >
              Example
            </Link>
            <Link
              className="nav-list"
              to="page1"
              showHeightActive={['10%', '60%']}
              toShowHeight // 	点击时是否滚动到 showHeightActive 的值上 就是停在 '10%', '60%'
              onFocus={this.onFocus}
              onBlur={this.onBlur}
            >
              Example2
            </Link>
            <Link
              className="nav-list"
              to="page2"
              showHeightActive={['60%', '50%']}
              toShowHeight
              onFocus={this.onFocus}
              onBlur={this.onBlur}
            >
              Example3
            </Link>
            <Link
              className="nav-list"
              to="page3"
              offsetTop={100}
              onFocus={this.onFocus}
              onBlur={this.onBlur}
            >
              Example4
            </Link>
            <div
              ref={c => {
                this.bar = c;
              }}
              className="nav-bar"
            />
          </div>
        </div>
        <Element
          className="pack-page page0"
          id="page0"
          onChange={this.onChange}
        >
          <QueueAnim className="home-title">
            <div className="page-title" key="title">
              <p>
                {_package.name}@{_package.version}
              </p>
            </div>
            <div className="page-description" key="c">
              <p>The link demo</p>
            </div>
          </QueueAnim>
        </Element>
        <ScrollOverPack id="page1" className="page1">
          <TweenOne className="tween-one" key="0" animation={{ opacity: 1 }}>
            默认进入与出场
          </TweenOne>
          <QueueAnim key="1">
            <div key="0" className="demo-content" />
            <div
              key="1"
              className="demo-content"
              style={{ backgroundColor: '#F38EAD' }}
            />
            <div key="2" className="demo-content" />
            <div key="3" className="demo-content" />
          </QueueAnim>
        </ScrollOverPack>

        <ScrollOverPack
          className="pack-page page2"
          style={{ backgroundColor: '#0098CE' }}
          always={false}
          id="page2"
        >
          <div className="page2-title">只进入一次</div>
          <Animate key="0" transitionName="fade" transitionAppear>
            <div className="demo-content2" />
          </Animate>
          <TweenOne
            className="demo-content2"
            animation={{ y: 0, opacity: 1 }}
            key="1"
            style={{ transform: 'translateY(100px)', opacity: 0 }}
          />
        </ScrollOverPack>

        <ScrollOverPack
          className="pack-page page3"
          style={{ backgroundColor: '#174270' }}
          playScale={0.8}
          id="page3"
        >
          <TweenOne
            animation={{ opacity: 1 }}
            style={{ opacity: 0 }}
            key="title"
            className="page2-title"
          >
            在页面80％时进入
          </TweenOne>
          <Animate key="0" transitionName="fade" transitionAppear>
            <div className="demo-content" />
          </Animate>
          <TweenOne
            className="demo-content"
            animation={{ y: 0, opacity: 1 }}
            key="1"
            style={{ transform: 'translateY(100px)', opacity: 0 }}
          />
        </ScrollOverPack>
      </div>
    );
  }
}

export default Demo;
